package com.xxx.car.web.admin;

import com.github.pagehelper.Page;
import com.xxx.car.entity.vo.request.ReqChargeDetailQueryForAdmin;
import com.xxx.car.entity.vo.request.ReqChargeTotalStatistics;
import com.xxx.car.entity.vo.request.ReqChargeUserStatistics;
import com.xxx.car.entity.vo.response.RespChargeGiveDetail;
import com.xxx.car.entity.vo.response.RespChargeTotalStatistics;
import com.xxx.car.entity.vo.response.RespChargeUserStatistics;
import com.xxx.car.service.TradeService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by xuwenjie on 2017/6/27.
 */
@Controller
@RequestMapping("/admin/charge")
public class AdminChargeController {

    @Autowired
    private TradeService tradeService;

    @RequestMapping("/")
    public String index() {
        return "charge";
    }

    @RequestMapping("/total")
    @ResponseBody
    public AdminResponse total(@Valid @ModelAttribute ReqChargeTotalStatistics reqChargeTotalStatistics, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        RespChargeTotalStatistics statistics = tradeService.chargeTotalStatistics(reqChargeTotalStatistics);
        return AdminResponse.returnSuccess(statistics);
    }

    @RequestMapping("/list")
    @ResponseBody
    public AdminResponse list(@Valid @ModelAttribute ReqChargeUserStatistics reqChargeUserStatistics, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespChargeUserStatistics> page = tradeService.chargeUserStatistics(reqChargeUserStatistics);
        return AdminResponse.returnSuccess(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @RequestMapping("/chargeDetail")
    @ResponseBody
    public AdminResponse chargeDetail(@Valid @ModelAttribute ReqChargeDetailQueryForAdmin reqChargeDetailQueryForAdmin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespChargeGiveDetail> detailPage = tradeService.findChargeDetail(reqChargeDetailQueryForAdmin);
        return AdminResponse.returnSuccess(detailPage.getPageNum(), detailPage.getPageSize(), detailPage.getPages(), detailPage.getTotal(), detailPage.getResult());
    }
}
